package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.compents.PageResult;
import com.oracle.mapper.PhPetshopMapper;
import com.oracle.pojo.PhPetshop;
import com.oracle.pojo.PhPetshopExample;
import com.oracle.pojo.PhPetshopExample.Criteria;
import com.oracle.service.PetshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class PetshopServiceImpl implements PetshopService {

	@Autowired
	private PhPetshopMapper petshopMapper;

	/**
	 * 查询全部
	 */
	@Override
	public List<PhPetshop> findAll() {
		return petshopMapper.selecPhyExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<PhPetshop> page=   (Page<PhPetshop>) petshopMapper.selecPhyExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(PhPetshop petshop) {
		petshopMapper.insert(petshop);
	}


	/**
	 * 修改
	 */
	@Override
	public void update(PhPetshop petshop){
		petshopMapper.updateByPrimaryKey(petshop);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public PhPetshop findOne(Integer id){
		return petshopMapper.selecPhyPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] ids) {
		for(Integer id:ids){
			petshopMapper.deleteByPrimaryKey(id);
		}
	}


		@Override
	public PageResult findPage(PhPetshop petshop, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		PhPetshopExample example=new PhPetshopExample();
		Criteria criteria = example.createCriteria();

		if(petshop!=null){
						if(petshop.getShopname()!=null && petshop.getShopname().length()>0){
				criteria.andShopnameLike("%"+petshop.getShopname()+"%");
			}			if(petshop.getShopaddr()!=null && petshop.getShopaddr().length()>0){
				criteria.andShopaddrLike("%"+petshop.getShopaddr()+"%");
			}			if(petshop.getTelphone()!=null && petshop.getTelphone().length()>0){
				criteria.andTelphoneLike("%"+petshop.getTelphone()+"%");
			}
		}

		Page<PhPetshop> page= (Page<PhPetshop>)petshopMapper.selecPhyExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
